package com.mrent.web.controller.pay;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.GoodsOrderInfo;
import com.mrent.entity.Order;
import com.mrent.entity.OrderManager;
import com.mrent.service.GoodsOrderService;
import com.mrent.service.OrderManagerService;
import com.mrent.service.OrderService;
import com.mrent.util.ConfigHelper;
import com.mrent.util.Constance;
import com.mrent.util.Utils;
import com.tenpay.MediPayResponseHandler;
import com.tenpay.util.TenpayUtil;

@Controller
public class PayNotifyController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private GoodsOrderService goodsOrderService;
	
	@Autowired
	private OrderManagerService orderManagerService;
	
	private static java.util.logging.Logger logger = Logger.getLogger(PayNotifyController.class.getName());
	
	/**
	 * 财付通支付返回通知.
	 * 
	 * @return
	 */
	@RequestMapping("/pay/notify/tenpay")
	public String tenpay(HttpServletRequest request,HttpServletResponse response) throws IOException{
		logger.info("pay notify tenpay !");
		String key = ConfigHelper.getPropertyValue("tenpay.key");
		MediPayResponseHandler resHandler = new MediPayResponseHandler(request, response);
		resHandler.setKey(key);
		if(resHandler.isTenpaySign()) {
			String cft_tid = resHandler.getParameter("cft_tid");//交易单号.
			String mch_vno = resHandler.getParameter("mch_vno");
			String retcode = resHandler.getParameter("retcode");//支付结果.
			String status = resHandler.getParameter("status");//状态码.
			logger.info("mch_vno is:" + mch_vno);
			if( "0".equals(retcode) ) {
				int iStatus = TenpayUtil.toInt(status);
				Order order = orderService.getOrder(mch_vno);
				if(order != null){//租店申请类订单.只处理交易成功状态.
					switch(iStatus) {
						case 5:		//买家收货确认，交易成功
							orderHandle(order);
							break;
					}
				}else{//商品订单.处理各种状态.
					GoodsOrderInfo goodsOrder = goodsOrderService.getGoodsOrder(mch_vno);
					switch(iStatus) {
						case 1:		//交易创建
							
							break;
						case 2:		//收获地址填写完毕
						
							break;
						case 3:		//买家付款成功，注意判断订单是否重复的逻辑
						
							break;
						case 4:		//卖家发货成功
						
							break;
						case 5:		//买家收货确认，交易成功
							
							break;
						case 6:		//交易关闭，未完成超时关闭
						
							break;
						case 7:		//修改交易价格成功
						
							break;
						case 8:		//买家发起退款
						
							break;
						case 9:		//退款成功
						
							break;
						case 10:	//退款关闭
						
							break;
						default:
						
					}
				}
				
				//调用doShow, 打印meta值,告诉财付通处理成功.
				resHandler.doShow();
			} else {
				System.out.println("支付失败");
			}
		} else {
			System.out.println("认证签名失败");
		}
		return null;
	}
	
	/**
	 * 支付宝支付返回通知.
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/pay/notify/alipay")
	public String alipay(HttpServletRequest request,HttpServletResponse response){
		
		
		
		return null;
	}
	
	/**
	 * 租店申请订单处理.
	 * 
	 */
	private void orderHandle(Order order){
		order.setOrderState(Constance.ORDER_PAYED);
		orderService.updateOrder(order);
		//管理员相关.
		OrderManager orderManager = orderManagerService.getByOrderNo(order.getOrderNo());
		if(orderManager == null){
			orderManager = new OrderManager();
			orderManager.setOrderNo(order.getOrderNo());
			orderManager.setOrderMember(order.getOrderMember());
			orderManager.setOrderRequestYear(order.getOrderRequestYear());
			orderManager.setOrderRequestMonth(order.getOrderRequestMonth());
			orderManager.setOrderRequestDay(order.getOrderRequestDay());
			orderManager.setOrderEndYear(order.getOrderEndYear());
			orderManager.setOrderEndMonth(order.getOrderEndMonth());
			orderManager.setOrderEndDay(order.getOrderEndDay());
			orderManager.setState(Constance.ORDER_PAYED);
			orderManager.setLoginPwd(RandomStringUtils.randomAlphanumeric(10));
			orderManagerService.saveOrderManager(orderManager);
			
			//发送邮件.
		}else {
			orderManager.setState(Constance.ORDER_PAYED);
			orderManagerService.modifyOrderManager(orderManager);
		}
	}

}
